#include "mainwindow.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QListView>
#include <QDesktopWidget>
#include <QApplication>
#include <QGuiApplication>
#include <QScreen>
#include <QStandardItemModel>
#include <QStringListModel>
#include <QMessageBox>
#include <QModelIndex>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QVBoxLayout* vbox = new QVBoxLayout();
    _lview = new QListView();

    initListView();
    vbox->addWidget(_lview);

    getDesktopSize();
    maxmumSize();
    showMaximized();



    setLayout(vbox);
}

void MainWindow::initListView(){
    _itemmodel = new QStandardItemModel(this);
    QStringList strList;
    strList.append("A");
    strList.append("B");
    strList.append("C");
    strList.append("D");
    strList.append("E");
    strList.append("F");
    strList.append("G");

    int nCount = strList.size();
    for(int i = 0; i < nCount; i++)
    {
       QString string = static_cast<QString>(strList.at(i));
       QStandardItem *item = new QStandardItem(string);
       _itemmodel->appendRow(item);
    }
    _lview->setModel(_itemmodel);
    _lview->setFixedSize(200,300);

    connect(_lview,SIGNAL(clicked(QModelIndex)),this,SLOT(listClick(QModelIndex)));
}

void MainWindow::listClick(QModelIndex index){
    QString strTemp;
    strTemp = index.data().toString();

    QMessageBox msg;
    msg.setText(strTemp);
    msg.exec();
}

void MainWindow::maxmumSize()
{
    setMaximumWidth(_width);
    setMaximumHeight(_height);
}

void MainWindow::getDesktopSize()
{
    ///已过时API
    ///获取桌面widget
    //QDesktopWidget* desktopWidget = QApplication::desktop();
    ///获取可用桌面大小
    //QRect deskRect = QApplication::desktop()->availableGeometry();
    ///获取主屏幕分辨率
    //QRect screenRect = QApplication::desktop()->screenGeometry();
    QScreen* screen = QGuiApplication::primaryScreen();
    _width = screen->geometry().width();
    _height = screen->geometry().height();
}

MainWindow::~MainWindow()
{
}

